SHELL=cmd.exe
# TODO:支持GNU ARM编译器 
# GNUCC=$(GNUARM_BINDIR)\arm-none-eabi-gcc.exe
# GNULD=$(GNUARM_BINDIR)\arm-none-eabi-ld.exe
# GNU_TOOL_BINDIR=$(GNUARM_TOOLDIR)\bin
# GNU_TOOL_INCDIR=$(GNUARM_TOOLDIR)\include
ARMCC_DIR=$(ARMCC_BINDIR)
ARMCC=$(ARMCC_DIR)\armcc.exe
ARMLD=$(ARMCC_DIR)\armlink.exe
ARMAR=$(ARMCC_DIR)\armar.exe
ARMELF=$(ARMCC_DIR)\fromelf.exe
ARMASM=$(ARMCC_DIR)\armasm.exe
CCFLAGS=--cpu=ARM920T --arm --littleend --c99 --apcs=/interwork -I"$(AIMCC_INCDIR)" -O0 -g --diag_suppress=1,1296 --diag_style=gnu
ASMFLAGS=--cpu=ARM920T -I.\include --arm --littleend -g --apcs=/interwork --diag_suppress=A1608 --diag_style=gnu

outDir=$(addsuffix /Debug,$(CURDIR))
outDirAtOs=$(subst /,\,$(outDir))
scatterFile=$(addprefix $(subst /,\,$(CURDIR))\,startup.sct)

targetAxf=$(outDir)/startup.axf
targetBin=$(outDir)/startup.bin

#Debug目录下需存在对应uVision的工程
keilV5Exe=$(KEIL_BINDIR)\UV4.exe
KeilProjectFile=$(outDirAtOs)\S3c2440.uvproj

srcAsmFiles=2440init.s ExceptionHandler.s cp15.s

srcCFiles=main.c \
	cpu.c \
	mmu.c \
	clibrary_retarget.c \
	led.c \
	keyboard.c \
	sys_dbg.c \
	interrupt.c \
	uart.c

objs=$(addprefix $(outDir)/,$(srcCFiles:%.c=%.o) $(srcAsmFiles:%.s=%.o))

$(targetBin):$(targetAxf)
	@$(ARMELF) --bin --output=$(targetBin) $(targetAxf)
$(targetAxf):$(objs) makeoutdir
	@$(ARMLD) --scatter=$(scatterFile) --entry=__START --info=totals -o $(targetAxf) $(objs)
$(outDir)/%.o:%.c makeoutdir
	@$(ARMCC) $(CCFLAGS) -c $< -o $@

$(outDir)/%.o:%.s makeoutdir
	@$(ARMASM) $(ASMFLAGS) -o $@ $<

.PHONY:makeoutdir clean run

makeoutdir:
	@if not exist $(outDirAtOs) @mkdir $(outDirAtOs)
clean:makeoutdir
	@-del $(outDirAtOs)\*.o $(outDirAtOs)\*.axf $(outDirAtOs)\*.bin
run:$(KeilProjectFile)
	@$(keilV5Exe) $(KeilProjectFile)